package com.jhws.business.face.controller;

import com.jhws.business.comm.service.ITblBuildingDimService;
import com.jhws.business.face.bo.FaceConstant;
import com.jhws.business.face.bo.FaceInfo;
import com.jhws.business.face.service.FaceService;
import com.jhws.common.base.bo.JsonRespData;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@RequestMapping("/face")
@Controller
public class FaceController {

    @Resource
    private FaceService faceService;
    @Resource
    private ITblBuildingDimService buildingDimService;

    /**
     * 添加权限
     *
     * @param ownerCode 业主ID
     * @param building  楼栋
     * @param unit      单元
     * @return
     * @throws Exception
     */
    @RequestMapping("addFaceAuth.x")
    @ResponseBody
    public JsonRespData addFaceAuth(String ownerCode, String building, String unit) throws Exception {
        FaceInfo faceInfo = faceService.addFaceAuth(ownerCode, building, unit);
        if (faceInfo != null) {
            Map<String, Object> map = new HashMap<>();
            String buildingAliasName = buildingDimService.getBuildingAliasName(faceInfo.getCommCode(), faceInfo.getBuilding(), faceInfo.getUnit());
            String unitAliasName = buildingDimService.getUnitAliasName(faceInfo.getCommCode(), faceInfo.getBuilding(), faceInfo.getUnit());
            map.put("faceInfo", faceInfo);
            map.put("buildingAliasName", buildingAliasName);
            map.put("unitAliasName", unitAliasName);
            return new JsonRespData("", 1, map);
        }
        return new JsonRespData("", -1, "权限分配错误！");
    }

    /**
     * 根据业主ID查询人脸信息
     *
     * @param ownerCode
     * @return
     */
    @RequestMapping("queryFaceInfoByOwnerCode.x")
    @ResponseBody
    public JsonRespData queryFaceInfoByOwnerCode(String ownerCode) {
        List<FaceInfo> faceInfos = faceService.getFaceInfosByWhere("where owner_code = '" + ownerCode + "' and state !='" + FaceConstant.STATE_CODE.delete.ordinal() + "'", true);
        List<Map<String, Object>> maps = new ArrayList<>();
        if (faceInfos != null) {
            Map<String, Object> map;
            //查询别名
            for (FaceInfo faceInfo : faceInfos) {
                map = new HashMap<>();
                String building = buildingDimService.getBuildingAliasName(faceInfo.getCommCode(), faceInfo.getBuilding(), faceInfo.getUnit());
                String unit = buildingDimService.getUnitAliasName(faceInfo.getCommCode(), faceInfo.getBuilding(), faceInfo.getUnit());
                map.put("faceInfo", faceInfo);
                map.put("buildingAliasName", building);
                map.put("unitAliasName", unit);
                maps.add(map);
            }
        }
        return new JsonRespData("", 1, maps);
    }

    /**
     * 跟新状态
     *
     * @param id
     * @param state
     * @return
     * @throws Exception
     */
    @RequestMapping("updateFaceState.x")
    @ResponseBody
    public JsonRespData updateFaceState(String id, int state) throws Exception {
        faceService.updateFaceState(id, state);
        return new JsonRespData("", 1, "成功");
    }

    /**
     * 删除人脸信息
     *
     * @param id
     * @return
     */
    @RequestMapping("deleteFaceById.x")
    @ResponseBody
    public JsonRespData deleteFaceById(String id) throws Exception {
        faceService.deleteFaceInfoByWhere("where id ='" + id + "'", false);
        return new JsonRespData("", 1, "成功");
    }
}
